//
//  BIDCreateNoticeViewController.m
//  商务厅
//
//  Created by mal on 13-12-16.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDCreateNoticeViewController.h"
#import "BIDLoginViewController.h"
#import "BIDCustomTextView.h"
#import "BIDMemberListViewController.h"
#import "BIDCustomTextField.h"

static NSString *createNoticeURL = @"noticeAndroid!addNotice.action";

@interface BIDCreateNoticeViewController ()<BIDMemberListViewControllerDelegate>

@end

@implementation BIDCreateNoticeViewController

@synthesize receiverTextField;
@synthesize queryBtn;
@synthesize contentTextView;
@synthesize submitBtn;
@synthesize textViewHeight;
@synthesize memberIDs;
@synthesize memberNames;
@synthesize noticeContent;
@synthesize noticeTitle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"新建通知";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    memberIDs = @"";
    
    self.contentTextView.text = noticeContent;
    _titleTF.text = noticeTitle;
    
    [_titleTF setBackgroundColor:[UIColor clearColor]];
    _titleTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    _titleTF.layer.borderWidth = 1.f;
    _titleTF.inputAccessoryView = self.toolBar;
    //
    
    [receiverTextField setBackgroundColor:[UIColor clearColor]];
    receiverTextField.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    receiverTextField.layer.borderWidth = 1.f;
    receiverTextField.inputAccessoryView = self.toolBar;
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar.png"]];
    receiverTextField.rightView = imgView;
    receiverTextField.rightViewMode = UITextFieldViewModeAlways;
    
    //设置textView
    contentTextView.placeholder = @"请输入通知内容...";
    [contentTextView setBackgroundColor:[UIColor clearColor]];
    contentTextView.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    contentTextView.layer.borderWidth = 1.f;
    contentTextView.inputAccessoryView = self.toolBar;
    
    [BIDGlobalMethods setImgForBtn:submitBtn imgNameForNormal:@"blueBtnBg_normal.png" imgNameForHighlight:@"blueBtnBg_highlight.png"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    receiverTextField = nil;
    queryBtn = nil;
    contentTextView = nil;
    submitBtn = nil;
}

- (IBAction)tapDown:(id)sender
{
    [contentTextView resignFirstResponder];
}

- (IBAction)textFieldEditEnd:(id)sender
{
}
//选取接收人
- (IBAction)queryBtnHandler:(id)sender
{
    BIDMemberListViewController *vc = [[BIDMemberListViewController alloc] init];
    vc.chooseType = TYPE_CHECK;
    vc.delegate = self;
    vc.bShowAllOption = YES;
    vc.rightNavigationItemTitle = @"确定";
    vc.choosedMemberIdArr = [memberIDs componentsSeparatedByString:@","];
    [self.navigationController pushViewController:vc animated:YES];
}

//提交
- (IBAction)submitBtnHandler:(id)sender
{
    if(memberIDs.length==0)
    {
        [BIDGlobalMethods showAlertView:@"请先选择接收人员" buttonTitle:@"关闭" delegate:nil tag:0];
        return;
    }

    if(contentTextView.text.length==0)
    {
        [BIDGlobalMethods showAlertView:@"内容不能为空" buttonTitle:@"关闭" delegate:nil tag:0];
        return;
    }
    if(_titleTF.text.length==0)
    {
        [BIDGlobalMethods showAlertView:@"请输入通知的标题" buttonTitle:@"关闭" delegate:nil tag:0];
        return;
    }
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], createNoticeURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"notice.title=%@&notice.createUser=%@&notice.content=%@&receiveId=%@", _titleTF.text, [AppDelegate getUserId], contentTextView.text, memberIDs];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if([responseObj isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *dic = (NSDictionary*)responseObj;
            NSNumber *result = dic[@"isSuccess"];
            if([result intValue] == 1)
            {
                [BIDGlobalMethods showAlertView:@"发送成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"发送失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

- (void)passValue:(NSString *)str1 param:(NSString *)str2
{
    //names,ids
    memberNames = str1;
    memberIDs = str2;
    self.receiverTextField.text = str1;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 1)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    if([contentTextView isFirstResponder])
    {
        NSDictionary *info = notification.userInfo;
        CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        NSInteger keyboardHeight = keyboardSize.height;
        CGFloat value1 = CGRectGetHeight(self.view.frame)-keyboardHeight;
        CGFloat value2 = CGRectGetMaxY(contentTextView.frame);
        if(value1 < value2)
        {
            NSValue *durationValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
            NSTimeInterval timeInterval;
            [durationValue getValue:&timeInterval];
            [UIView beginAnimations:@"animation" context:nil];
            [UIView setAnimationDuration:timeInterval];
            _bottomForTextViewConstraint.constant = value2 - value1 + 24;
            [self.view layoutIfNeeded];
            [UIView commitAnimations];
        }
    }
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    _bottomForTextViewConstraint.constant = 24;
    [self.view layoutIfNeeded];
}

#pragma mark - BIDMemberListViewControllerDelegate
- (void)getChoosedMemberIdForNotice:(NSString *)ids andNames:(NSString *)names
{
    memberIDs = ids;
    receiverTextField.text = names;
}

@end
